#include "friendapplybox.h"
#include "ui_friendapplybox.h"

const QString ApprovedStyle="QPushButton{background-color:transparent;}";
const QString UnderReviewStyle="\
QPushButton{background-color: rgb(162, 215, 168);\
            border:2px solid rgb(0,0,0);border-radius:10px;\
            color: rgb(255, 255, 255);}\
QPushButton:hover{background-color: rgb(152, 205, 158);}";

FriendApplyBox::FriendApplyBox(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FriendApplyBox)
{
    ui->setupUi(this);
}

FriendApplyBox::~FriendApplyBox()
{
    delete ui;
}

//初始化控件
void FriendApplyBox::initBox(const QString& useroc,const QString &friendoc, const QString &friendname, const EnumLib::ApplyStatus status)
{
    this->useroc=useroc;
    this->friendoc=friendoc;
    this->friendname=friendname;
    this->status=status;
    ui->label_oc->setText(friendoc);
    ui->label_name->setText(friendname);
    ui->label_avatar->setPixmap("./User/"+useroc+"/Cache/"+friendoc+".png");
    setBtnMode(status);
}

void FriendApplyBox::setStatus(const EnumLib::ApplyStatus status)
{
    this->status=status;
    setBtnMode(status);
}

QSize FriendApplyBox::getSizeHint()
{
    return this->sizeHint();
}

//设置按钮状态，同意-已同意
void FriendApplyBox::setBtnMode(const EnumLib::ApplyStatus status)
{
    switch (status) {
    case EnumLib::ApplyStatus::APPROVED:
        ui->pushButton->setText("已同意");
        ui->pushButton->setStyleSheet(ApprovedStyle);
        ui->pushButton->setEnabled(false);
        break;
    case EnumLib::ApplyStatus::UNDERREVIEW:
        ui->pushButton->setText("同意");
        ui->pushButton->setStyleSheet(UnderReviewStyle);
        break;
    case EnumLib::ApplyStatus::TARGETAPPROVED:
        ui->pushButton->setEnabled(true);
        qDebug()<<"set 已通过";
        ui->pushButton->setText("已通过");
        ui->pushButton->setStyleSheet(ApprovedStyle);
        break;
    case EnumLib::ApplyStatus::TARGETUNDERREVIEW:
        ui->pushButton->setEnabled(true);
        ui->pushButton->setText("等待通过");
        ui->pushButton->setStyleSheet(ApprovedStyle);
        break;
    }

}

//同意按钮按下
void FriendApplyBox::on_pushButton_clicked()
{
    connect(AvatarLib::getInstance(),&AvatarLib::s_avatarAppend,this,&FriendApplyBox::emitCloseSignal);
    //发送同意好友申请的报文
    ThreadManger::getInstance()->sendFriendResult(friendoc);
    //移动tmp中的头像到avatar中，因为在接收好友申请时就将头像暂存到tmp中
    ThreadManger::getInstance()->moveAvatarCacheToAvatar(friendoc);
    //将好友申请缓冲区的好友状态更新为已同意
    FriendApplyLib::getInstance()->setApply(friendoc,EnumLib::ApplyStatus::APPROVED);
    //更新数据库中的好友申请状态为已同意
    ThreadManger::getInstance()->upDataFriendApplyStatus(friendoc,EnumLib::ApplyStatus::APPROVED);
    //向数据库中添加联系人
    ThreadManger::getInstance()->addContact(friendoc,friendname);
    //向联系人缓冲区中追加联系人
    //ContactLib::getInstance()->appendContact(friendoc,friendname);
    //向头像缓冲区中追加头像
    ThreadManger::getInstance()->appendAvatar(FriendListLib::getInstance()->getUserOC(),friendoc);
    //修改按钮样式
    setBtnMode(EnumLib::ApplyStatus::APPROVED);

}

void FriendApplyBox::emitCloseSignal()
{
    disconnect(AvatarLib::getInstance(),&AvatarLib::s_avatarAppend,this,&FriendApplyBox::emitCloseSignal);
    //发送信号给好友申请窗口，在窗中代码中更新联系人列表
    emit s_appendContactWidget(friendoc,friendname);
}
